package com.yl.applicationsecurity.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yl.applicationsecurity.mapper.NationMapper;
import com.yl.applicationsecurity.mapper.PathMonitoringMapper;
import com.yl.applicationsecurity.pojo.InspectionConfiguration;
import com.yl.applicationsecurity.pojo.Nation;
import com.yl.applicationsecurity.pojo.PathMonitoring;
import com.yl.applicationsecurity.pojo.ScaVulnerability;
import com.yl.applicationsecurity.service.NationService;
import com.yl.applicationsecurity.service.PathMonitoringService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 鹤仙问鹿仙
 * @program: Application-security
 * @description:
 * @date 2025-04-23 18:15:00
 */
@Service
public class PathMonitoringServiceImpl  extends ServiceImpl<PathMonitoringMapper, PathMonitoring> implements PathMonitoringService {



    @Override
    public PageInfo<PathMonitoring> findListByPathMonitoring(String[] nationSelect, PathMonitoring pathMonitoring, Date startCreateDate, Date endCreateDate, Integer pageNum, Integer pageSize,Integer permissionCheck, Integer unauthorizedCheck) {
        PageHelper.startPage(pageNum, pageSize );//每页显示8条

        LambdaQueryWrapper<PathMonitoring> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if ( StringUtils.isNotEmpty(pathMonitoring.getUri())){
            lambdaQueryWrapper.like(PathMonitoring::getUri,pathMonitoring.getUri());
        }
        if ( StringUtils.isNotEmpty(pathMonitoring.getAppName())){
            lambdaQueryWrapper.like(PathMonitoring::getAppName,pathMonitoring.getAppName());
        }
        if (null!=nationSelect&&nationSelect.length!=0){
            lambdaQueryWrapper.in(PathMonitoring::getNation,nationSelect);
        }


        if(permissionCheck!=null){  //permissionCheck  为1 代表已进行权限检查，为0代表未进行权限检查

            if(permissionCheck==1){
                lambdaQueryWrapper.isNotNull(PathMonitoring::getPermissionCheckDate);

            }else if(permissionCheck==0){
                lambdaQueryWrapper.isNull(PathMonitoring::getPermissionCheckDate);
            }
        }

        if(unauthorizedCheck!=null){ //unauthorizedCheck  为1 代表已进行未授权检查，为0代表未进行未授权检查
            if(permissionCheck==1){
                lambdaQueryWrapper.isNotNull(PathMonitoring::getUnauthorizedCheckDate);
            }else if(permissionCheck==0){
                lambdaQueryWrapper.isNotNull(PathMonitoring::getUnauthorizedCheckDate);
            }
        }


        if(startCreateDate!=null){
            lambdaQueryWrapper.ge(PathMonitoring::getCreateDate, startCreateDate);
        }


        if(endCreateDate!=null){
            lambdaQueryWrapper.le(PathMonitoring::getCreateDate, endCreateDate);
        }
        lambdaQueryWrapper.orderByDesc(PathMonitoring::getCreateDate);//根据首次发现时间排序

        List<PathMonitoring> list = baseMapper.selectList(lambdaQueryWrapper);


        PageInfo<PathMonitoring> pageInfo = new PageInfo<PathMonitoring>(list);


        if (pageInfo.getPages() == 0) {
            pageInfo.setPages(1);
        }

        return pageInfo;
    }


    public int insertPathMonitoring(PathMonitoring pathMonitoring) {
        //首先查询下是否有历史数据
        LambdaQueryWrapper<PathMonitoring> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(PathMonitoring::getUri,pathMonitoring.getUri());



        if ( StringUtils.isNotEmpty(pathMonitoring.getUri())){
            lambdaQueryWrapper.eq(PathMonitoring::getUri,pathMonitoring.getUri());
        }
        if ( StringUtils.isNotEmpty(pathMonitoring.getAppName())){
            lambdaQueryWrapper.eq(PathMonitoring::getAppName,pathMonitoring.getAppName());
        }
        if (StringUtils.isNotEmpty(pathMonitoring.getNation())){
            lambdaQueryWrapper.eq(PathMonitoring::getNation,pathMonitoring.getNation());
        }
        if (StringUtils.isNotEmpty(pathMonitoring.getRequestMethod())){
            lambdaQueryWrapper.eq(PathMonitoring::getRequestMethod,pathMonitoring.getRequestMethod());
        }

        if (baseMapper.selectCount(lambdaQueryWrapper)!=0){
            return 0;
        }

        pathMonitoring.setId(UUID.randomUUID().toString());
        pathMonitoring.setCreateDate(new Date());//创建时间
        pathMonitoring.setUnauthorizedCheckDate(null);
        pathMonitoring.setPermissionCheckDate(null);
        if(pathMonitoring.getUri()!=null&pathMonitoring.getAppName()!=null&pathMonitoring.getNation()!=null){
            return baseMapper.insert(pathMonitoring);
        }
        return 0;
    }

    @Override
    public int insertCheckStatus(PathMonitoring pathMonitoring) {
        //判断下关键参数是否存在
        if(StringUtils.isEmpty(pathMonitoring.getUri())||StringUtils.isEmpty(pathMonitoring.getAppName())||StringUtils.isEmpty(pathMonitoring.getNation())){
            return 0;
        }



        //查询数据是否已经存在
        LambdaQueryWrapper<PathMonitoring> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PathMonitoring::getUri,pathMonitoring.getUri());
        lambdaQueryWrapper.eq(PathMonitoring::getAppName,pathMonitoring.getAppName());
        lambdaQueryWrapper.eq(PathMonitoring::getNation,pathMonitoring.getNation());
        PathMonitoring pathMonitoring2 = baseMapper.selectOne(lambdaQueryWrapper);

        //如果存在则修改
        if (pathMonitoring2!=null){
            LambdaUpdateWrapper<PathMonitoring> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            if(pathMonitoring.getPermissionCheckDate()!=null){
                lambdaUpdateWrapper.set(PathMonitoring::getPermissionCheckDate,pathMonitoring.getPermissionCheckDate());
            }

            if(pathMonitoring.getUnauthorizedCheckDate()!=null) {
                lambdaUpdateWrapper.set(PathMonitoring::getUnauthorizedCheckDate, pathMonitoring.getUnauthorizedCheckDate());
            }
            if(pathMonitoring.getUnauthorizedCheckDate()!=null) {
                lambdaUpdateWrapper.set(PathMonitoring::getFullPath, pathMonitoring.getFullPath());
            }

            lambdaUpdateWrapper.in(PathMonitoring::getId,pathMonitoring2.getId());
            baseMapper.update(null,lambdaUpdateWrapper);
        }else{//如果不存在则添加
            pathMonitoring.setId(UUID.randomUUID().toString());
            pathMonitoring.setCreateDate(new Date());//创建时间
            baseMapper.insert(pathMonitoring);
        }



        return 1;
    }


}

